/*
 * @lc app=leetcode.cn id=572 lang=cpp
 *
 * [572] 另一棵树的子树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
#include <bits/stdc++.h>
using namespace std;
class Solution {
    bool _isSubTree(TreeNode* root, TreeNode* subRoot)
    {
        if(root == nullptr)
        {
            if(subRoot == nullptr) return true;
            else return false;
        }
        if(subRoot == nullptr) return false;
        if(root->val != subRoot->val) return false;
        if(_isSubTree(root->left, subRoot->left) && _isSubTree(root->right, subRoot->right))
        {
            return true;
        }
        return false;
    }
    bool findRoot(TreeNode* root, TreeNode* subRoot)
    {
        if(root == nullptr) return false;
        if(root->val == subRoot->val)
        {
            if(_isSubTree(root, subRoot)) return true;
        }
        return findRoot(root->left, subRoot) || findRoot(root->right, subRoot);
    }
public:
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        return findRoot(root, subRoot);
    }
};
// @lc code=end

